package org.test.java;

import static org.test.java.ShuaTa.httpRequestWithLogFile;
import static org.test.java.ShuaTa.logFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class Meditation {

	private String _sid;
	private Properties props;

	public Meditation(String _sid, Properties props) {
		this._sid = _sid;
		this.props = props;
	}

	public void meditation(int minCoins) throws IOException {
		Map map = httpRequestWithLogFile(_sid, "user.php", "GetUserinfo", "",
				logFile);
		Map data = (Map) map.get("data");
		System.out.println(data.get("NickName").toString());
		Map meditation = httpRequestWithLogFile(_sid, "meditation.php", "Info",
				"", logFile);
		if (getInt(meditation, "status") != 1) {
			System.out.println(meditation);
			httpRequestWithLogFile(_sid, "meditation.php", "Deal", "", logFile);
			System.out.println("deal,退出。");
			return;
		}
		Map meditationData = (Map) meditation.get("data");
		if (getInt(meditationData, "Coins") < minCoins) {
			System.out.println("钱少，退出。");
			return;
		}
		int tempId = 0;
		for (Object o : ((List) meditationData.get("NpcList"))) {
			if (o == null)
				continue;
			tempId = Math
					.max(tempId, (Double.valueOf(o.toString())).intValue());
		}
		while (true) {
			meditation = httpRequestWithLogFile(_sid, "meditation.php", "Npc",
					"NpcId=" + tempId, logFile);
			if (getInt(meditation, "status") != 1) {
				System.out.println(meditation);
				httpRequestWithLogFile(_sid, "meditation.php", "Deal", "", logFile);
				System.out.println("deal。");
				continue;
			}
			meditationData = (Map) meditation.get("data");
			if (getInt(meditationData, "Coins") < minCoins) {
				System.out.println("钱少，退出。");
				return;
			}
			tempId = 0;
			for (Object o : ((List) meditationData.get("NpcList"))) {
				if (o == null)
					continue;
				tempId = Math.max(tempId,
						(Double.valueOf(o.toString())).intValue());
			}
		}
	}

	private int getInt(Map map, String key) {
		return (Double.valueOf(map.get(key).toString())).intValue();
	}

}
